package com.ctsi.organise.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.organise.common.constant.PartyStatus;
import com.ctsi.organise.entity.PartyEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;


/**
 * PartyEntity Service 类
 * @author   zhangjw
 * @description  PartyEntity
 * @created	 Sun Nov 11 12:02:44 CST 2018
 */

@Repository
public class PartyEntityManager {
	private static final Logger logger=LoggerFactory.getLogger(PartyEntityManager.class);
	@Autowired
	private MybatisGenericDao dao;
	
	/**
	 * 添加
	 */
	public void insertPartyEntity(PartyEntity partyEntity){
		if(UtilValidate.isEmpty(partyEntity.getId()))
		{
			partyEntity.setId(UUIDGenerator.getId());
		}
		if (partyEntity.getOrderNo() == null) {
            int max = (int) dao.queryForObject(PartyEntity.class.getName()+".findMaxOrderWithTypeId", partyEntity.getPartyTypeId());
            partyEntity.setOrderNo(max+1);
        }
		logger.debug("insert {}",partyEntity);
		dao.insert(partyEntity);
	}	
	/**
	 * 动态更新
	 */	
	public int updatePartyEntityByPKSelective(PartyEntity partyEntity){
		logger.debug("updateByPrimaryKeySelective {}",partyEntity);
		return dao.update(PartyEntity.class.getName()+".updateByPrimaryKeySelective", partyEntity);
	}
	/**
	 * 更新
	 */
	public int updatePartyEntity(PartyEntity partyEntity){
		logger.debug("update {}",partyEntity);
		return dao.update(partyEntity);
	}
	
	
	public PartyEntity editPartyEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("editById {}",id);
		return (PartyEntity)dao.queryForObject(PartyEntity.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 PartyEntity对象
	 */
	public PartyEntity findPartyEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("findById {}",id);
		return dao.get(PartyEntity.class, id);
	}
	/**
	 * 删除PartyEntity对象
	 */
	public int removePartyEntity(String id){	
		if(UtilValidate.isNotEmpty(id))
			return dao.delete(PartyEntity.class.getName()+".delete", id);
			//return dao.remove(id);
		return 0;
	}
	
	/**
	 * 详细页查询
	 */
	public PartyEntity findByDetail(String id){
		return (PartyEntity)dao.queryForObject(PartyEntity.class.getName()+".findByDetail",id);
	}
	/** 
	 * 多条件查询结果 
	 */
	public Page<PartyEntity> findByMulitCondition(Map<String,Object> param , int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1)
			return dao.queryPage(PartyEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/** 
	 * 多条件查询结果不分页
	 */
	public List<PartyEntity> find(Map<String,Object> map){
		logger.debug("find {}",map);
		return (List<PartyEntity>)dao.queryForList(PartyEntity.class.getName()+".selectByMap",map);
	}

    /**
     * 逻辑删除
     * @param partyId
     */
    public void logicallyDelete(String partyId){
        PartyEntity party = new PartyEntity();
        party.setId(partyId);
        party.setStatusId(PartyStatus.deleted.value);
        this.updatePartyEntityByPKSelective(party);
    }


}
